﻿function showLogin(handler) {
	if (refreshWin('showLogin')) return;
	var username = new Ext.form.TextField({
				fieldLabel : '用户名/学号',
				anchor : '95%',
				minLength : 5,
				maxLength : 20,
				allowBlank : false,
				enableKeyEvents : true
			});
	var password = new Ext.form.TextField({
				fieldLabel : '密码',
				anchor : '95%',
				minLength : 5,
				maxLength : 20,
				allowBlank : false,
				inputType : 'password',
				enableKeyEvents : true
			});
	username.on('keypress', onNext);
	password.on('keypress', onNext);
	var btnlogin = new Ext.Button({
				text : '登陆',
				iconCls : 'ok-icon',
				handler : onLogin
			});
	var toolbar = new Ext.Toolbar({
				items : [new Ext.Button({
									text : '校外注册',
									iconCls : 'reg-icon',
									disabled : !sys.settings.openOutsideReg,
									handler : onReg
								}), new Ext.Button({
									text : '忘记密码',
									iconCls : 'password-icon',
									disabled : true,
									handler : onForget
								}), new Ext.Toolbar.Fill(), btnlogin]
			});
	var label = new Ext.form.Label({
				text : '（学生用户初次登录使用本科教学平台的密码）'
			});
	var form = new Ext.FormPanel({
				frame : true,
				buttonAlign : 'left',
				labelAlign : 'right',
				labelWidth : 80,
				bodyStyle : 'padding-top:5px',
				items : [username, password, {
							bodyStyle : 'text-align:center;padding-bottom:5px;padding-top:5px',
							items : label
						}],
				fbar : toolbar
			});
	var win = new Ext.Window({
				id : 'showLogin',
				title : '用户登录',
				iconCls : 'login-icon',
				manager : ui.windows,
				width : 320,
				minWidth : 280,
				autoHeight : true,
				items : form
			});
	win.show();
	username.focus(true, 400);

	function onReg() {
		showReg();
		win.close();
	}
	function onForget() {}
	function onLogin() {
		if (!form.getForm().isValid()) {
			alertx('请正确输入数据！');
			return;
		}
		doLogin({
					username : username.getValue(),
					password : password.getValue(),
					success : function() {
						win.close();
						if (handler) handler();
					}
				});
	}
	function onNext(t, e) {
		if (e.getKey() == e.ENTER) {
			if (t == username)
				password.focus();
			else {
				btnlogin.focus();
				onLogin();
			}
		}
	}
}

function doLogin(obj) {
	ajax({
				url : url.doLogin,
				params : {
					username : obj.username,
					password : obj.password
				},
				success : function(response) {
					if (response.student) {
						response.student.success = obj.success;
						response.student.password = obj.password;
						showReg(response.student);
					} else {
						refreshMainPage()
						if (obj.success) obj.success();
					}
				}
			});
}
